package com.hc.conferencecheck.controller;

import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.service.ImageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户图片处理的controller
 */
@RestController
@RequestMapping("/imageController")
public class ImageController {
    @Resource
    private ImageService imageServiceImpl;

    /**
     * 更新服务器上的某张图片
     */
    @RequestMapping("/updateImageFromServer")
    public ResultMsg updateImageFromServer(@RequestParam("image") MultipartFile file, @RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        ResultMsg resultMsg = imageServiceImpl.updateImageFormServer(file, type, identifyId);
        return resultMsg;
    }

    /**
     * 删除服务器上的某张图片
     */
    @RequestMapping("/deleteImageFormServer")
    public ResultMsg deleteImageFormServer(@RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        ResultMsg resultMsg = imageServiceImpl.deleteImageFormServer(type, identifyId);
        return resultMsg;
    }

    /**
     * 读取显示服务器上的图片
     */
    @RequestMapping("/showIamgeFromServer")
    public void showIamgeFromServer(HttpServletResponse response, @RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        imageServiceImpl.showIamge(response, type, identifyId);
    }

    /**
     * 保存图片到服务器
     */
    @RequestMapping("/uploadImageToServer")
    public ResultMsg uploadImageToServer(@RequestParam("image") MultipartFile file, @RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        ResultMsg resultMsg = imageServiceImpl.uploadImageToServer(file, type, identifyId);
        return resultMsg;
    }

    /**
     * 更新某张图片
     */
    @RequestMapping("/updateImage")
    public ResultMsg updateImage(@RequestParam("image") MultipartFile file, @RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        ResultMsg resultMsg = imageServiceImpl.updateImage(file, type, identifyId);
        return resultMsg;
    }


    /**
     * 显示某张图片
     */
    @RequestMapping("/showImage")
    public void showImage(@RequestParam("type") int type, @RequestParam("identifyId") int identifyId, HttpServletResponse response) {
        imageServiceImpl.showImage(type, identifyId, response);
    }

    /**
     * 上传图片到数据库
     */
    @RequestMapping("/uploadImage")
    public ResultMsg uploadImage(@RequestParam("image") MultipartFile file, @RequestParam("type") int type, @RequestParam("identifyId") int identifyId) {
        ResultMsg resultMsg = imageServiceImpl.uploadImage(file, type, identifyId);
        return resultMsg;
    }
}
